package com.tailyou.hms.firstapp.act;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.util.CommonUtils;

/**
 * 对话框具体使用可参考
 * https://blog.csdn.net/m0_58941767/article/details/125107288?spm=1001.2014.3001.5502
 */
public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        findViewById(R.id.btnAlertDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
        findViewById(R.id.btnCustomDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });
        findViewById(R.id.btnPrgDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPrgDialog();
            }
        });
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
        builder.setTitle("Title").setMessage("Write your message here.").setCancelable(true)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CommonUtils.showToast(DialogActivity.this, "Click Yes");
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CommonUtils.showToast(DialogActivity.this, "Click No");
                    }
                });
        builder.create().show();
    }

    private void showCustomDialog() {
        Dialog dialog = new Dialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_login, null);
        Button btnLogin = view.findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(v -> {
            CommonUtils.showToast(DialogActivity.this, "点击登录");
            dialog.cancel();
        });
        dialog.setContentView(view);
        Window window = dialog.getWindow();

        // 设置宽高
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.9);
        layoutParams.height = 1500; // 设置高度为500像素
        window.setAttributes(layoutParams);

        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置透明背景以显示圆角效果
        // 创建圆角形状
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE); // 设置形状为矩形
        gradientDrawable.setColor(Color.WHITE); // 设置背景颜色
        gradientDrawable.setCornerRadius(32); // 设置圆角半径，根据需要调整数值
        // 将圆角形状设置为对话框的背景
        window.setBackgroundDrawable(gradientDrawable);

        dialog.show();
    }

    private void showPrgDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("下载");
        progressDialog.setMessage("正在下载，请稍后...");
        progressDialog.setCancelable(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
    }

}